// run: $exec < input
#include <cstdio>
#include <algorithm>
#include <deque>
#include <vector>
#include <utility>

long long ans, n, d;

std::vector<std::pair<int, long long>> data;

int main()
{
	std::scanf("%I64d %I64d", &n, &d);
	data.resize(n);
	for (int i = 0; i < n; i++)
		std::scanf("%d %I64d", &data[i].first, &data[i].second);
	std::sort(data.begin(), data.end());
	std::deque<std::pair<int, long long>> dq;
	long long sum = 0;
	for (int i = 0; i < n; i++) {
		while (!dq.empty() && data[i].first - dq.front().first >= d) {
			sum -= dq.front().second;
			dq.pop_front();
		}
		dq.push_back(data[i]);
		sum += data[i].second;
		ans = std::max(ans, sum);
	}
	std::printf("%I64d", ans);
}

